//
//  STUserManger.m
//  GodHorses
//
//  Created by Mac on 2017/11/25.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "STUserManger.h"
#define TM_USER_JSON_KEY @"TM_USER_JSON_KEY"

#define TM_USER_ACCOUNT_KEY @"TM_USER_ACCOUNT_KEY"
#define TM_USER_PWD_KEY @"TM_USER_PWD_KEY"
@implementation STUserManger
+ (STUserManger *)defult{
    static STUserManger * deflut = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        deflut = [STUserManger new];
    });
    return deflut;
}

- (void)removeUserPreferce{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:TM_USER_JSON_KEY];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:TM_USER_ACCOUNT_KEY];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:TM_USER_PWD_KEY];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)updateUserModel:(MCUserModel *)userModel{
    if (userModel) {
        if ([self loginedUser]) {
            //不更新token
            userModel.ry_token = [self loginedUser].ry_token;
        }

        NSString * userString = userModel.mj_JSONString;
        [[NSUserDefaults standardUserDefaults] setValue:userString forKey:TM_USER_JSON_KEY];
    }else{
        NSLog(@"user 不能为空");
    }
}
- (MCUserModel *)loginedUser{
    NSString * jsonStr = [[NSUserDefaults standardUserDefaults] valueForKey:TM_USER_JSON_KEY];
    NSData * data = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    if (jsonStr.length) {
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        MCUserModel *loginedUser = [MCUserModel mj_objectWithKeyValues:dic];
        return loginedUser;
    }
    return nil;
}
- (void)updateAccount:(NSString *)account pwd:(NSString *)pwd{
    if (account.length && pwd.length) {
        [[NSUserDefaults standardUserDefaults] setValue:account forKey:TM_USER_ACCOUNT_KEY];
        [[NSUserDefaults standardUserDefaults] setValue:pwd forKey:TM_USER_PWD_KEY];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }

}
- (NSString*)account{
    NSString * account = [[NSUserDefaults standardUserDefaults] valueForKey:TM_USER_ACCOUNT_KEY];
    return account;
}
- (NSString*)pwd{
    NSString * pwd = [[NSUserDefaults standardUserDefaults] valueForKey:TM_USER_PWD_KEY];
    return pwd;
}
@end

